package com.example.imageandvideo.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.camera.cameralibrary.util.ContentUriUtil
import com.example.imageandvideo.R
import com.example.imageandvideo.adapter.NineImageAdapter
import com.example.imageandvideo.bean.NineImageBean
import com.example.imageandvideo.template.NineImageTemplate
import kotlinx.android.synthetic.main.activity_nine_image.*

/**
 *
 * @ClassName: NineImageClick
 * @Author: szj
 * @CreateDate: 6/24/21 2:15 PM
 * @UpdateRemark: 9宫格图片
 * @Version: 1.0
 */
class NineImageActivity : AppCompatActivity() {

    lateinit var adapter: NineImageAdapter

    //右上角按钮(0/9)
    lateinit var tvCount: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_nine_image)

        val list = ArrayList<NineImageBean>()

        //默认显示布局 2
        list.add(NineImageBean("", 2))

        val nineImageTemplate = NineImageTemplate(list)
        nineImageTemplate.template(this, recyclerView)

        adapter = nineImageTemplate.getAdapter() as NineImageAdapter

        //获取头布局控件
        tvCount = adapter.headerLayout.findViewById(R.id.tv_count)

        //底部布局
        buildBottomLayout()

        //接口回调数据
        adapter.mOnClick = object : NineImageAdapter.OnClick {

            //点击最后图片添加图片(最多能添加 9 张)
            override fun onItemClick(count: Int) {
                if (count <= 9) {
                    val intent = Intent()
                    intent.type = "image/*"
                    intent.action = Intent.ACTION_GET_CONTENT
                    startActivityForResult(intent, 100)
                } else {
                    toast("不能在添加了")
                }
            }

            //删除按钮回调
            @SuppressLint("SetTextI18n")
            override fun onDeleteClick(count: Int) {
                toast(count)
                adapter.remove(count)
                tvCount.text = "${adapter.data.size - 1} / 9"
                adapter.notifyDataSetChanged()
            }
        }
    }

    //底部按钮
    private fun buildBottomLayout() {
        val tvShow = adapter.footerLayout.findViewById<TextView>(R.id.tv_show)
        val tvInfo = adapter.footerLayout.findViewById<TextView>(R.id.tv_info)

        tvShow.setOnClickListener {
            tvInfo.text = adapter.data.joinToString("\n\n") {
                it.url
            }
        }

    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100) {
            val path = ContentUriUtil.getPath(this, data?.data)
            if (path != "") {
                adapter.addData(0, NineImageBean(path, 1))
                tvCount.text = "${adapter.data.size - 1}/9"
                adapter.notifyDataSetChanged()
            }
        }
    }
}

private fun NineImageActivity.toast(s: Any) {
    Toast.makeText(this, "$s", Toast.LENGTH_SHORT).show()
}
